//
//  JYTextView.m
//  TestNIMSDK
//
//  Created by jianyong.wang on 2017/6/5.
//  Copyright © 2017年 jianyong.wang. All rights reserved.
//

#import "JYTextView.h"

@implementation JYTextView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
-(void)deleteBackward
{
    if (self.selectedRange.length>0) {
        [super deleteBackward];
    }else if ([self emojiTextComplete])
    {
        if ([self.jy_Delegate respondsToSelector:@selector(jyTextViewDeleteBackward:)]) {
            [self.jy_Delegate jyTextViewDeleteBackward:self];
        }
    }else {
        [super deleteBackward];
    }
}
//判断表情字符的完整性  [大笑]
- (BOOL)emojiTextComplete{
    //判断光标位置的上一字符是否为‘]’，若不是则直接return No，走系统删除方法  ，如果存在，那么继续判断，倒叙查找，‘]’若优先于‘[’查找到，reutrn No，走系统删除方法,若查找到'[',则走自定义的方法
    BOOL exist = [[self.text substringWithRange:NSMakeRange(self.selectedRange.location-1, 1)] isEqualToString:@"]"];
    if (!exist) {
        return NO;
    }
    NSUInteger endLocation = self.selectedRange.location -1;
    for (long i = endLocation; i >= 0; i --) {
        char suff = [self.text characterAtIndex:i];
        if (i<endLocation && suff == ']') {
            return NO;
        }
        if (suff == '[') {
            return YES;
        }
    }
    return NO;
}
@end
